package cz.jarec.sender;

import cz.jarec.sender.exception.InvalidPhoneNumberException;
import cz.jarec.sender.exception.SenderInternalException;

/**
 * Send sms
 * 
 */
public class App {

	private static final String PORTAL_URL = "http://support.fialanet.cz";

	private static final String USERNAME = "crhonek.jaroslav24";
	private static final String PASSWORD = "871025";

	public static void main(String... args) {

		if (args.length < 2) {
			System.out
					.println("Provide phone number as first parameter and text as second");
			System.exit(1);
		}

		Sender sender = new SenderImpl();

		sender.setPassword(PASSWORD);
		sender.setPortalUrl(PORTAL_URL);
		sender.setUsername(USERNAME);

		try {
			sender.sendSms(args[0], buildMessageText(args));
		} catch (InvalidPhoneNumberException e) {
			System.err.println("You entered invalid phone number! Error: "
					+ e.getMessage());
			System.exit(1);
		} catch (SenderInternalException e) {
			System.err.println("SMS not send! Internal error! Error: "
					+ e.getMessage());
			e.printStackTrace();
			System.exit(1);
		}

	}

	private static String buildMessageText(String[] args) {

		StringBuilder sb = new StringBuilder();

		for (int i = 1; i < args.length; i++) {
			sb.append(args[i]);
			if (i != args.length - 1) {
				sb.append(' ');
			}
		}

		return sb.toString();
	}
}
